package com.liangwei.jd.service;

import com.google.common.collect.Lists;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.image.BufferedImage;
import java.util.List;
import java.util.Random;

/**
 * @author liangweih
 * @since 2019/3/14 15:42
 */

public class CommonlyUtil {
    private static final Logger log = LoggerFactory.getLogger(CommonlyUtil.class);

    public static void sleep(long millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void moves(WebDriver driver, WebElement element, int xDis) throws InterruptedException {
        int moveX = new Random().nextInt(8) - 5;
        int moveY = 0;
        Actions actions = new Actions(driver);
        new Actions(driver).clickAndHold(element).perform();
        Thread.sleep(180);
        actions.moveToElement(element, moveX, moveY).perform();
        int s;
        for (int i = 0; i < 20; i++) {
            s = 10;
            if (i % 2 == 0) {
                s = -10;
            }

            actions.moveToElement(element, s, 0).perform();
            Thread.sleep(new Random().nextInt(121) + 150);
        }
        actions.moveByOffset(xDis, 0).perform();
        Thread.sleep(new Random().nextInt(600) + 110);
        actions.release(element).perform();
    }

    public static void move(WebDriver driver, WebElement element, int xDis) throws InterruptedException {
        int xd;
        xd = new Random().nextInt(xDis / 8);
        Actions actions = new Actions(driver);
        new Actions(driver).clickAndHold(element).perform();
        xDis = (int) Math.round(xDis /1.555);
        int moveX = (int) (Math.random() * (xDis));
        int suiji = xDis - moveX;
        List<Integer> ls = randomX(suiji, suiji-1);
        actions.moveToElement(element, xd, xd + 2).perform();
        for (int i = 0; i < ls.size(); i++) {
            Thread.sleep(new Random().nextInt(30 * (i + 1)) + 100);
            if (i % 2 == 0) {
                if (i == 0) {
                    actions.moveByOffset(ls.get(i) + 8 - xd, i).perform();
                } else {
                    actions.moveByOffset(ls.get(i), i).perform();
                }
            } else {

                actions.moveToElement(element, (ls.get(i) + 8), 0).perform();
            }
        }
        List<Integer> lsa = randomX(moveX + xd, moveX > 1 ? moveX : 2);
        for (int i = 0; i < lsa.size(); i++) {
            Thread.sleep(new Random().nextInt(200) + 100);
            actions.moveByOffset(lsa.get(i), 0).perform();
            System.out.println("ls.get(i)22-"+lsa.get(i));
        }
        if (xd > 0) {
            Thread.sleep(new Random().nextInt(300) + 100);
            actions.moveByOffset(-xd, 0).perform();

        }

        actions.release(element).perform();
        Thread.sleep(2000l);

    }

    public static void move2019417(WebDriver driver, WebElement element, int xDis) throws InterruptedException {
        System.out.println("xdis"+xDis);
        int xd = 5;
        if (xDis % 4 == 0) {
            xd = new Random().nextInt(xDis / 8);
        }
        Actions actions = new Actions(driver);
        new Actions(driver).clickAndHold(element).perform();
        xDis = (int) Math.round(xDis / 1.275);
        int moveX = new Random().nextInt(xDis - 50);
        int suiji = xDis - moveX;
        List<Integer> ls = randomX(suiji, 45);
        actions.moveToElement(element, xd, xd + 2).perform();
        for (int i = 0; i < ls.size(); i++) {

            if (i % 2 == 0) {
                if (i == 0) {
                    System.out.println("ls.get(i) + 19 - xd"+(ls.get(i) + 19 - xd));
                    actions.moveByOffset(ls.get(i) + 19 - xd, 0).perform();
                } else {
                    System.out.println("ls.get(i)"+ls.get(i));
                    actions.moveByOffset(ls.get(i), 0).perform();
                }
            } else {
                System.out.println("ls.get(i) + 19"+ls.get(i) + 19);
                actions.moveToElement(element, ls.get(i) + 19, 0).perform();
            }
        }
        List<Integer> lsa = randomX(moveX + xd, moveX > 20 ? 20 : 10);
        for (int i = 0; i < lsa.size(); i++) {
            Thread.sleep(new Random().nextInt(200 * i + 1) + 100);
            actions.moveByOffset(lsa.get(i), 0).perform();
            System.out.println("ls.get(i)22"+ls.get(i));
        }
        System.out.println("-xd"+xd);
        Thread.sleep(new Random().nextInt(300) + 100);
        actions.moveByOffset(-xd, 0).perform();
        actions.release(element).perform();
    }


    public static List<Integer> randomX(int n, int onen) {

        int y = n;
        Random rand = new Random();
        List<Integer> list = Lists.newArrayList();
        if (n == 0) {
            log.error("n is 0 n is 0");
            list.add(1);
            return list;
        }
        if (n > onen) {
            log.error("n dayu onen n dayu onen");
            onen = n - 1;
        }
        int c, sum = 0,q=0;
        while (true) {
            q++;
            if(q>20)
            {
                log.error("q da yu20 {}",q);
                list.add(1);
                return list;

            }
            c = rand.nextInt(onen);
            if (c <1) {
                log.error("n{},,onen{},c{}",n,onen,c);
                continue;
            }
            n = n - c;

            if (n > 0) {
                sum = sum + c;
                list.add(c);
                continue;
            } else break;

        }

        if (y - sum > 0) {
            list.add(y - sum);
        }
        return list;
    }


    public static int getI(BufferedImage fullBI, BufferedImage bgBI) {


        for (int i = 0; i < bgBI.getWidth(); i++) {
            for (int j = 0; j < bgBI.getHeight(); j++) {
                if (difference(rgbCom(fullBI, i, j), rgbCom(bgBI, i, j)) > 255) {
                    return i;
                }
            }
        }
        return 0;
    }

    private static int[] rgbCom(BufferedImage fullBI, int i, int j) {
        int[] fullRgb = new int[3];
        fullRgb[0] = (fullBI.getRGB(i, j) & 0xff0000) >> 16;
        fullRgb[1] = (fullBI.getRGB(i, j) & 0xff00) >> 8;
        fullRgb[2] = (fullBI.getRGB(i, j) & 0xff);
        return fullRgb;
    }

    private static int difference(int[] a, int[] b) {
        return Math.abs(a[0] - b[0]) + Math.abs(a[1] - b[1]) + Math.abs(a[2] - b[2]);
    }

}
